package day01_IO_JDBC.demo_JDBC_emp_pro.Dao.Impl;

import com.mysql.cj.protocol.Resultset;
import day01_IO_JDBC.demo_JDBC_emp_pro.DBUtil.DBPoollUtils;
import day01_IO_JDBC.demo_JDBC_emp_pro.Dao.Emp_dao;
import day01_IO_JDBC.demo_JDBC_emp_pro.Entity.Emp2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Emp_dao_Impl implements Emp_dao {

    /*
    * check
    * */
    @Override
    public List<Emp2> queryAll_emp2() {
//      定义集合
        List<Emp2> emp2s = null;

//        获取DButil连接
        Connection connection = DBPoollUtils.getConnection();
//        判断connection是否为空
        if (connection == null) {
        } else {
            try {
//            写sql语句
                String sql = """
                                select emp_no, emp_name, pwd, salary,permission from emp2
                               """;
//            预处理sql语句
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
//            执行sql语句
                ResultSet resultset = preparedStatement.executeQuery(sql);
//            实例化 ArrayList
                emp2s = new ArrayList<>();
//                循环
                while (resultset.next()) {
                    emp2s.add(new Emp2(resultset.getInt(1),
                            resultset.getString(2),
                            resultset.getString(3),
                            resultset.getDouble(4),
                            resultset.getInt(5)
                    ));
                }
                /*
                 * break循环/switch/判断代码块
                 * continue跳出本次循环
                 * return结束当前方法
                 */
                return emp2s;

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    return emp2s;
    }

//    queryByEmpno
    @Override
    public List<Emp2> queryByEmpno(int emp_no) {

        List<Emp2> emp1List= null;

        Connection connection= DBPoollUtils.getConnection();

        if (connection == null ){
            System.out.println("连接失败");
        }
        else {
            try {
                String sql= """
                    select emp_no, emp_name, pwd, salary, permission  from emp2 where emp_no=?
                    """;
                PreparedStatement preparedStatement= connection.prepareStatement(sql);
                preparedStatement.setInt(1,emp_no);

                ResultSet resultSet=preparedStatement.executeQuery();

                emp1List= new ArrayList<>();

                while (resultSet.next()){
                    emp1List.add(
                            new Emp2(
                                    resultSet.getInt(1),
                                    resultSet.getString(2),
                                    resultSet.getString(3),
                                    resultSet.getDouble(4),
                                    resultSet.getInt(5)
                            )
                    );
                }
                return emp1List;

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        return emp1List;
    }

//    add_emp2
    @Override
    public int add_emp2(Emp2 emp2) {
        Connection connection= DBPoollUtils.getConnection();

        if (connection == null){
            System.out.println("连接失败");
        }
        else {
            try {
                String sql= """
                        insert into emp2(emp_no, emp_name, pwd, salary, permission) values(?,?,?,?,?)
                        """;
                PreparedStatement preparedStatement= connection.prepareStatement(sql);

                preparedStatement.setInt(1,emp2.getEmp_no());
                preparedStatement.setString(2,emp2.getEmp_name());
                preparedStatement.setString(3,emp2.getPwd());
                preparedStatement.setDouble(4,emp2.getSalary());
                preparedStatement.setInt(5,emp2.getPermission());

                int num= preparedStatement.executeUpdate();

                if (num>0){
                    System.out.println("成功执行增加操作");
                    return 1;
                }
                else {
                    System.out.println("操作执行失败");
                    return 0;
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    return 0;
    }

//    delete_emp2
    @Override
    public boolean delete_emp2(int emp_no) {
        Connection connection= DBPoollUtils.getConnection();

        if (connection == null){
            System.out.println("连接失败");
        }else {

            try {
                String sql= """
                        delete from emp2 where emp_no=?
                        """;
                PreparedStatement preparedStatement= connection.prepareStatement(sql);

                preparedStatement.setInt(1,emp_no);

                int num = preparedStatement.executeUpdate();
                if (num>0){
                    System.out.println("删除操作执行成功");
                    return true;
                }else {
                    System.out.println("删除操作执行失败");
                    return false;
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return false;
    }

//      update_emp2
    @Override
    public boolean update_emp2(int emp_no, String emp_name, String pwd) {
        Connection connection=DBPoollUtils.getConnection();

        if (connection == null){
            System.out.println("连接失败");
        }
        else {

            try {
                String sql= """
                        update emp2 set emp_name=?, pwd=? where emp_no= ?
                        """;
                PreparedStatement preparedStatement= connection.prepareStatement(sql);

                preparedStatement.setString(1,emp_name);
                preparedStatement.setString(2,pwd);
                preparedStatement.setInt(3,emp_no);

                int num= preparedStatement.executeUpdate();

                if (num>0){
                    System.out.println("员工更新操作执行成功");
                    return true;
                }else {
                    System.out.println("员工更新操作执行失败");
                    return false;
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

//    admin_update_emp2


    @Override
    public boolean admin_update_emp2(Emp2 emp2) {
        Connection connection= DBPoollUtils.getConnection();

        if (connection == null){
            System.out.println("连接失败");
        }else {
            if (emp2 != null){

                try {
                    String sql= """
                        update emp2 set emp_name=?, pwd=?, salary=?, permission=? where emp_no= ?
                        """;
                    PreparedStatement preparedStatement= connection.prepareStatement(sql);

                    preparedStatement.setString(1,emp2.getEmp_name());
                    preparedStatement.setString(2,emp2.getPwd());
                    preparedStatement.setDouble(3,emp2.getSalary());
                    preparedStatement.setInt(4,emp2.getPermission());
                    preparedStatement.setInt(5,emp2.getEmp_no());

                    int num= preparedStatement.executeUpdate();
                    if (num>0){
                        System.out.println("管理员更新执行操作成功");
                        return true ;
                    }else {
                        System.out.println("管理员更新执行操作失败");
                        return false;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

//    login_emp2

    @Override
    public int login_emp2(int emp_no, String emp_name, String pwd, int permission) {
        Connection connection= DBPoollUtils.getConnection();

        if (connection == null){
            System.out.println("连接失败");
        }else{

            try {
                String sql= """
                        select emp_name, pwd, salary, permission from emp2 where emp_name=? and pwd=? and permission=?
                        """;
                PreparedStatement preparedStatement= connection.prepareStatement(sql);

                preparedStatement.setString(1,emp_name);
                preparedStatement.setString(2,pwd);
                preparedStatement.setInt(3,permission);

                ResultSet resultSet= preparedStatement.executeQuery();
                if (resultSet.next()){
                    System.out.println("登录成功");
                    return 1;
                }
                else{
                    System.out.println("登陆失败");
                    return 0;
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return 0;
    }
}
